package com.bawei.common.vm

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.bawei.common.intent.BaseViewIntent
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.launch

/**
 * Author: Jason Zhang
 * Date: 2024/12/12
 * Desc: ViewModel基类
 */
abstract class BaseViewModel<I: BaseViewIntent> : ViewModel(){

    var channel: Channel<I> = Channel(Channel.UNLIMITED)

    init {
        viewModelScope.launch {
            //接收View层Intent
            channel.consumeAsFlow().collect{
                dispatchIntent(it)
            }
        }
    }

    /**
     * 分发意图
     */
    abstract fun dispatchIntent(intent: I)


}